#include "stdafx.h"
#include "EvgenyFace.h"

CEvgenyFace::CEvgenyFace(UINT bmpID)
{
	m_bmpID = bmpID;
}

CEvgenyFace::~CEvgenyFace(void)
{
}

/////////////////////////////////////////////////////////////////////////////
// Evgeny Savitsky'97

void CEvgenyFace::Paint( CDC *pDC, CWnd *pWnd ) 
{
	hMemDC = CreateCompatibleDC( pDC->m_hDC );
/*
	SelectPalette(dc.m_hDC,hPalette,TRUE);
	RealizePalette(dc.m_hDC);
*/			
	SelectPalette(hMemDC,hPalette,TRUE);
	RealizePalette(hMemDC);

	SelectObject(hMemDC, hFace);

	RECT rect;
	pWnd->GetClientRect( &rect );

/*
	BitBlt(pDC->m_hDC, rect.right - bmpFace.bmWidth - 35, 10,
		   bmpFace.bmWidth, bmpFace.bmHeight,
		   hMemDC, 0, 0, SRCCOPY);
*/
	BitBlt(pDC->m_hDC, 15, 10,
		   bmpFace.bmWidth, bmpFace.bmHeight,
		   hMemDC, 0, 0, SRCCOPY);

	DeleteDC( hMemDC );
}

HBITMAP CEvgenyFace::LoadResourceBitmap(HINSTANCE hInstance, LPSTR lpString,
                           HPALETTE FAR* lphPalette)
{
    HRSRC  hRsrc;
    HGLOBAL hGlobal;
    HBITMAP hBitmapFinal = NULL;
    LPBITMAPINFOHEADER  lpbi;
    HDC hdc;
    int iNumColors;
 
    if (hRsrc = FindResource(hInstance, lpString, RT_BITMAP))
       {
       hGlobal = LoadResource(hInstance, hRsrc);
       lpbi = (LPBITMAPINFOHEADER)LockResource(hGlobal);
 
       hdc = ::GetDC(NULL);
       *lphPalette =  CreateDIBPalette ((LPBITMAPINFO)lpbi, &iNumColors);
       if (*lphPalette)
          {
          SelectPalette(hdc,*lphPalette,FALSE);
          RealizePalette(hdc);
          }
 
       hBitmapFinal = CreateDIBitmap(hdc,
                   (LPBITMAPINFOHEADER)lpbi,
                   (LONG)CBM_INIT,
                   (LPSTR)lpbi + lpbi->biSize + iNumColors * sizeof(RGBQUAD),
                   (LPBITMAPINFO)lpbi,
                   DIB_RGB_COLORS );
 
       ::ReleaseDC(NULL,hdc);
       UnlockResource(hGlobal);
       FreeResource(hGlobal);
       }
    return (hBitmapFinal);
}

HPALETTE CEvgenyFace::CreateDIBPalette (LPBITMAPINFO lpbmi, LPINT lpiNumColors)
{
   LPBITMAPINFOHEADER  lpbi;
   LPLOGPALETTE     lpPal;
   HANDLE           hLogPal;
   HPALETTE         hPal = NULL;
   int              i;
 
   lpbi = (LPBITMAPINFOHEADER)lpbmi;

   if (lpbi->biBitCount <= 8)
       *lpiNumColors = (1 << lpbi->biBitCount);
   else
       *lpiNumColors = 0;  // No palette needed for 24 BPP DIB


   if (*lpiNumColors)
      {
      hLogPal = GlobalAlloc (GHND, sizeof (LOGPALETTE) +
                             sizeof (PALETTEENTRY) * (*lpiNumColors));
      lpPal = (LPLOGPALETTE) GlobalLock (hLogPal);
      lpPal->palVersion    = 0x300;
      lpPal->palNumEntries = *lpiNumColors;
 
      for (i = 0;  i < *lpiNumColors ;  i++)
      {

         lpPal->palPalEntry[i].peRed   = lpbmi->bmiColors[i].rgbRed + 0;
         lpPal->palPalEntry[i].peGreen = lpbmi->bmiColors[i].rgbGreen + 0;
         lpPal->palPalEntry[i].peBlue  = lpbmi->bmiColors[i].rgbBlue + 0;
         lpPal->palPalEntry[i].peFlags = 0;
	   }

      hPal = CreatePalette (lpPal);
      GlobalUnlock (hLogPal);
      GlobalFree   (hLogPal);
   }
   return hPal;
}

int CEvgenyFace::Initialize(CPalette *pPalette)
{
	hFace = LoadResourceBitmap(AfxGetInstanceHandle(),
								MAKEINTRESOURCE( m_bmpID ), &hPalette);
	GetObject( hFace, sizeof(BITMAP), &bmpFace);

//	CPalette pal;
	pPalette = CPalette::FromHandle( hPalette );

	return 0;
}

